package ConsumerProducers;

import java.util.ArrayList;

public class Desk {

    ArrayList<String> arrayList = new ArrayList<>();

    public synchronized void put() throws InterruptedException {
        String name = Thread.currentThread().getName();
        if (arrayList.isEmpty()){
            arrayList.add(name + " 放入一个包子");
            System.out.println(name + "成功放入一个包子");
            Thread.sleep(2000);
            this.notifyAll();
            this.wait();
        }else {
            this.notifyAll();
            this.wait();
        }
    }

    public synchronized  void take() throws InterruptedException {
        String name = Thread.currentThread().getName();
        if ( !arrayList.isEmpty()){
            String s = arrayList.get(0);
            arrayList.clear();
            System.out.println(name + "拿到了:" + s);
            Thread.sleep(2000);
            this.notifyAll();
            this.wait();
        }else {
            this.notifyAll();
            this.wait();
        }

    }

}
